package cn.lili.util;

import cn.hutool.core.lang.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * mq 发送工具
 *
 * @author liushuai(liushuai711 @ gmail.com)
 * @version v4.0
 * @Description:
 * @since 2023/2/14 10:43
 */
@RequiredArgsConstructor
@Component
@Slf4j
public class AmqpSender {

    private final AmqpTemplate amqpTemplate;

    public void send(AmqpMessage amqpMessage) {
//        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());

        // 等待消息发送的确认信息
        try {

            log.debug("发送消息到队列：{}，路由键：{}，消息：{}", amqpMessage.getExchange(), amqpMessage.getRoutingKey(), amqpMessage.getMessage());
            amqpTemplate.convertAndSend(amqpMessage.getExchange(), amqpMessage.getRoutingKey(), amqpMessage.getMessage());
//            correlationData.getFuture().get(10, TimeUnit.SECONDS);
        } catch (Exception e) {
            // 处理异常
            e.printStackTrace();
        }
    }


}
